package gr.handmademarket.ui.panels;

import static gr.handmademarket.util.CommonStatics.PROFILE_PICS_PATH;
import gr.handmademarket.po.implementations.Product;
import gr.handmademarket.po.implementations.PurchaseItem;
import gr.handmademarket.po.implementations.Seller;
import gr.handmademarket.po.interfaces.Category;
import gr.handmademarket.ui.WebHandMadeSession;
import gr.handmademarket.ui.links.CategoryLink;
import gr.handmademarket.ui.pages.CategoryPage;
import gr.handmademarket.ui.pages.MyBasketPage;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.NonCachingImage;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.resource.ContextRelativeResource;

/**
 * the panel of the product.
 */
public class ProductPanel 
extends Panel{

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * the name label.
	 */
	@SuppressWarnings("nls")
	private Label nameLabel = new Label("name");
	
	/**
	 * the description label.
	 */
	@SuppressWarnings("nls")
	private Label descriptionLabel = new Label("description");
	
	/**
	 * the product.
	 */
	private Product product;
	
	/**
	 * Creates a new ProductPanel object. 
	 *
	 * @param id
	 * @param model
	 */
	@SuppressWarnings("nls")
	public ProductPanel(String id, IModel<Product> model) {
		super(id, model);
		product = model.getObject();
		String path = "/images/products/" + product.getCode() + ".jpg"; 
		ContextRelativeResource resource = new ContextRelativeResource(path);
		NonCachingImage image = new NonCachingImage("img", resource);
		AjaxLink<NonCachingImage> basketLink = createBasketLink();
		
		Category category = product.getCategory();
		CategoryLink categoryLink = new CategoryLink("categoryLink");
		categoryLink.setCategory(category);
		Label categoryLabel = new Label("categoryLabel",category.getName());
		categoryLink.add(categoryLabel);
		
		Label priceLabel = new Label("price", product.getPrice().toString());
		
		AjaxLink<NonCachingImage> sellerLink = createSellerLink(product.getSeller());
		
		add(image,nameLabel, descriptionLabel,basketLink,categoryLink,priceLabel,sellerLink);
	}
	
	/**
	 * @return ajax basket link.
	 */
	@SuppressWarnings("nls")
	private AjaxLink<NonCachingImage> createBasketLink(){
		String path = "/images/basket-add-icon.png"; 
		ContextRelativeResource resource = new ContextRelativeResource(path);
		NonCachingImage image = new NonCachingImage("basketImg", resource);
		AjaxLink<NonCachingImage> ajaxLink = new AjaxLink<NonCachingImage>("basketLink", new Model<NonCachingImage>(image)) {
			/**
			 * serialVersionUID
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				WebHandMadeSession session = (WebHandMadeSession) WebHandMadeSession.get();
				PurchaseItem item = new PurchaseItem();
				item.setProduct(product);
				if (!session.getUserItems().contains(item)){
					session.getUserItems().add(item);
				}
				MyBasketPage page = new MyBasketPage();
				setResponsePage(page);
			}
		};
		ajaxLink.add(image);
		return ajaxLink;
	}
	
	/**
	 * @param seller 
	 * @return ajax seller link.
	 */
	@SuppressWarnings("nls")
	private AjaxLink<NonCachingImage> createSellerLink(Seller seller){
		final Seller localSeller = seller;
		String path = PROFILE_PICS_PATH + localSeller.getCode() + ".jpg"; 
		ContextRelativeResource resource = new ContextRelativeResource(path);
		NonCachingImage image = new NonCachingImage("sellerImg", resource);
		AjaxLink<NonCachingImage> ajaxLink = new AjaxLink<NonCachingImage>("sellerLink", new Model<NonCachingImage>(image)) {
			/**
			 * serialVersionUID
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				CategoryPage page = new CategoryPage(localSeller);
				setResponsePage(page);
			}
		};
		ajaxLink.add(image);
		return ajaxLink;
	}

}
